package com.shop.loan.handler;

import com.shop.loan.mapper.LoanOrderBillByMybatisMapper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.LoanOrderPayLogByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.LoanOrderPayLogByNoAuth;
import com.shop.loan.model.entity.*;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.model.enums.OrderStatus;
import com.shop.loan.model.enums.PayType;
import com.shop.loan.model.vo.LoanOrderPayLogDelVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.util.LoanOrderUtil;
import com.shop.loan.util.OpFailUtil;
import com.shop.loan.util.PasswordUtil;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class LoanOrderPayLogDelHandler implements OperationHandler<LoanOrderPayLogByNoAuth, LoanOrderPayLogDelVo> {

    @Resource
    EruptUserService eruptUserService;

    @Resource
    OpFailUtil opFailUtil;

    @Resource
    private HttpServletRequest request;

    @Resource
    LoanOrderPayLogByMybatisMapper loanOrderPayLogByMybatisMapper;

    @Resource
    LoanOrderBillByMybatisMapper loanOrderBillByMybatisMapper;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    LoanOrderUtil loanOrderUtil;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    RedisCache redisCache;

    @Override
    public String exec(List<LoanOrderPayLogByNoAuth> data, LoanOrderPayLogDelVo loanOrderPayLogDelVo, String[] param) {
        /**
         * 校验还款密码
         */
        SettingByMybatis setting = redisCache.getSetting(false);
        if (!PasswordUtil.checkPassword(loanOrderPayLogDelVo.getPassword(), setting.getPayPassword())) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserService.getCurrentUid(), OpFailType.pay);
            return "msg.error('无效还款密码!')";
        }
        LoanOrderPayLogByMybatis loanOrderPayLogByMybatis = loanOrderPayLogByMybatisMapper.selectByPrimaryKey(data.get(0).getId());
        if (loanOrderPayLogByMybatis == null) {
            return "msg.error('记录异常!')";
        }

        LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(loanOrderPayLogByMybatis.getOrderId());
        if (loanOrderByMybatis == null) {
            return "msg.error('记录异常!')";
        }

        List<LoanOrderBillByMybatis> loanOrderBillByMybatisList = loanOrderBillByMybatisMapper.selectListByOrder(loanOrderPayLogByMybatis.getOrderId());
        if (loanOrderBillByMybatisList == null) {
            return "msg.error('记录异常!')";
        }
        loanOrderPayLogByMybatis.setIsDelete(true);
        loanOrderPayLogByMybatis.setDeleteTime(new Date());
        loanOrderPayLogByMybatisMapper.updateByPrimaryKeySelective(loanOrderPayLogByMybatis);

        for (LoanOrderBillByMybatis loanOrderBillByMybatis : loanOrderBillByMybatisList) {
            BigDecimal billPayMoney = loanOrderPayLogByMybatisMapper.selectSumByBillId(loanOrderBillByMybatis.getId(), data.get(0).getId(), PayType.principal_interest.getType());
            loanOrderBillByMybatis.setPayMoney(billPayMoney == null ? BigDecimal.ZERO : billPayMoney);

            billPayMoney = loanOrderPayLogByMybatisMapper.selectSumByBillId(loanOrderBillByMybatis.getId(), data.get(0).getId(), PayType.goods.getType());

            LoanOrderBillByMybatis updateBill = new LoanOrderBillByMybatis();
            updateBill.setId(loanOrderBillByMybatis.getId());
            if (Util.checkIsSettle(loanOrderBillByMybatis)) {
                //本金+利息 <= 优惠金额+还款金额  则结清
                updateBill.setPayStatus(true);
                updateBill.setOverdueStatus(true);
                updateBill.setOverdueDay(0);
            } else {
                updateBill.setPayStatus(false);
            }
            updateBill.setPayMoney(loanOrderBillByMybatis.getPayMoney());
            loanOrderBillByMybatisMapper.updateByPrimaryKeySelective(updateBill);

            loanOrderBillByMybatis.setPayStatus(updateBill.getPayStatus());
            if (updateBill.getOverdueStatus() != null) {
                loanOrderBillByMybatis.setOverdueStatus(updateBill.getOverdueStatus());
            }
        }
        LoanOrderByMybatis updateOrder = new LoanOrderByMybatis();
        updateOrder.setId(loanOrderPayLogByMybatis.getOrderId());
        updateOrder.setPayMoney(loanOrderUtil.getBillPayMoney(loanOrderBillByMybatisList));
        updateOrder.setDiscountsMoney(loanOrderUtil.getBillDisMoney(loanOrderBillByMybatisList));
        updateOrder.setOverdueStatus(loanOrderUtil.checkBillOverdue(loanOrderBillByMybatisList));
        if (loanOrderUtil.checkBillEnd(loanOrderBillByMybatisList)) {
            //订单结束
            updateOrder.setStatus(OrderStatus.end.getStatus());
            updateOrder.setOverdueStatus(true);
            updateOrder.setFinishTime(new Date());
        } else {
            updateOrder.setStatus(OrderStatus.underway.getStatus());
        }
        if (loanOrderByMybatis.getStatus() == OrderStatus.end.getStatus().intValue()
                && !loanOrderUtil.checkBillEnd(loanOrderBillByMybatisList)) {
            //当前订单已经结束且现在账单未结束  则扣除用户完结次数
            userByMybatisMapper.updateUserEndNumBySub(loanOrderByMybatis.getUserId());
        }
        loanOrderByMybatisMapper.updateByPrimaryKeySelective(updateOrder);
        return null;
    }
}
